/**
 * Created by reyton on 16-12-7.
 */
//防止F5进行刷新
/*$(document).ready(function() {
 $(document).bind("keydown", function(e) {
 e = window.event || e;
 if (e.keyCode == 116) {
 if(!confirm("清除表单中的数据?")){
 e.keyCode = 0;
 return false;
 }
 }
 });
 });*/
//保存报销单
function save() {
    var myForm = document.getElementById("myForm");
    myForm.action = "claimAction_saveDetail.action";
    if (checkFields("addDetail")) {
        myForm.submit();
    } else {
        return false;
    }
}
//提交报销单
function submitAll() {
    var myForm = document.getElementById("myForm");
    myForm.action = "claimAction_submit.action";
    if (document.getElementById("claimVoucherId").value != "") {
        myForm.submit();
    } else {
        alert("请创建报销单");
    }
}
//添加报销单详细信息
function addDetail() {
    //var detailId = document.getElementById("bizClaimVoucherDetailId").value
    var myForm = document.getElementById("myForm");
    myForm.action = "claimAction_saveDetail.action";
    if (checkFields("addDetail")) {
        myForm.submit();
    }
}
//删除报销单详细信息
function delDetail(id) {
    if (confirm("要删除该条详细信息？")) {
        var myForm = document.getElementById("myForm");
        myForm.action = "claimAction_deleteDetail.action?bizClaimVoucherDetail.id=" + id;
        myForm.submit();
    } else {
        return;
    }
}
//修改
function edit(value, id) {
    value.parent().prevAll().children().last().replaceWith(
        "<select name = 'updateSelect'>" +
        "<option value='城际交通费'>城际交通费</option>" +
        "<option value='市内交通费'>市内交通费</option>" +
        "<option value='通讯费'>通讯费</option>" +
        "<option value='礼品费'>礼品费</option>" +
        "<option value='办公费'>办公费</option>" +
        "<option value='交际餐费'>交际餐费</option>" +
        "<option value='餐补'>餐补</option>" +
        "<option value='住宿费'>住宿费</option>" +
        "</select>");
    //移除其他明细的操作选项以防错误操作
    value.parent().parent().siblings().find(".operating").remove();
    $("#bizClaimVoucherDetailAccount").attr("disabled", "disabled");
    $("#bizClaimVoucherDetailDes").attr("disabled", "disabled");
    value.parent().prevAll().children().eq(1).attr("name", "updateAccount");
    value.parent().prevAll().children().eq(0).attr("name", "updateDes");
    //修改按钮属性
    value.children().attr("src", "/Images/sub.gif");
    value.children().attr("title", "保存报销单项");
    value.siblings().children().attr("src", "/Images/delete.gif");
    value.siblings().children().attr("title", "取消操作");
    //修改文本框状态为可见
    value.parent().prevAll().children(0).removeProp("readonly");
    value.parent().prevAll().children(0).removeProp("style");
    //更改点击事件为提交修改
    value.attr("onclick", "update(" + id + ")");
}
//提交修改
function update(id) {
    var myForm = document.getElementById("myForm");
    var item = $('[name = updateSelect]').val();
    var des = $('[name = updateDes]').val();
    var account = $('[name = updateAccount]').val();
    //验证非空
    if (checkFields("edit")) {
        var action = "claimAction_updateDetail.action?bizClaimVoucherDetail1.id=" + id +
            "&bizClaimVoucherDetail1.des=" + des +
            "&bizClaimVoucherDetail1.item=" + item +
            "&bizClaimVoucherDetail1.account=" + account;
        myForm.action = action;
        myForm.submit();
    }
}
//判空
function checkFields(opt) {
    var claimVoucherEvent = document.getElementById("event").value;

    var bizClaimVoucherDetailAccount = document.getElementById("bizClaimVoucherDetailAccount").value;

    var bizClaimVoucherDetailDes = document.getElementById("bizClaimVoucherDetailDes").value;

    var updateAccount = $('[name = updateAccount]').val();

    var updateDes = $('[name = updateDes]').val();

    if (opt == "addDetail") {
        if (claimVoucherEvent == null || claimVoucherEvent == "") {
            alert("事由不能为空");
            return false;
        } else if (bizClaimVoucherDetailAccount == null || bizClaimVoucherDetailAccount == "") {
            alert("金额不能为空");
            return false;
        } else if (bizClaimVoucherDetailDes == null || bizClaimVoucherDetailDes == "") {
            alert("费用说明不能为空");
            return false;
        } else {
            return true;
        }
    } else if (opt == "submitAll") {
        if (document.getElementById("claimVoucherId").value > 0) {
            return true;
        } else {
            if (claimVoucherEvent == null || claimVoucherEvent == "") {
                alert("事由不能为空");
                return false;
            } else if (bizClaimVoucherDetailAccount == null || bizClaimVoucherDetailAccount == "") {
                alert("金额不能为空");
                return false;
            } else if (bizClaimVoucherDetailDes == null || bizClaimVoucherDetailDes == "") {
                alert("费用说明不能为空");
                return false;
            } else {
                return true;
            }
        }
    } else if (opt == "edit") {
        if (updateAccount == null || updateAccount == "") {
            alert("金额不能为空");
            return false;
        } else if (updateDes == null || updateDes == "") {
            alert("费用说明不能为空");
            return false;
        } else {
            return true;
        }
    }
}
//获得当前系统时间
function setCurTime() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var timeString = year + "-" + month + "-" + day;
    var oCtl = document.getElementById("createTime");
    var view = document.getElementById("time");
    oCtl.value = timeString;
    if (document.all) {
        view.innerText = timeString;
    } else {
        view.textContent = timeString;
    }
}